#include "myThread.h"
#include <QDebug>

myThread::myThread(QWidget *parent)
{
    this->parent = parent;
}

myThread::~myThread()
{
    delete this->Socket;
}

void myThread::recvInfoSlot()
{
    /* 读取所有数据 */
    QString str = this->Socket->readAll();
    qDebug() << str << endl;

    /* 截取字符串 */
    QStringList list = str.split(":");

    /* 获取命令 */
    QString cmd = list.at(0);
    qDebug() << "命令:" << cmd << endl;

    /* 获取数据 */
    QString data = list.at(1);
    qDebug() << "数据:" << data << endl;

    /* 错误判断 */
    bool ok;
    u_long num = cmd.toInt(&ok);
    if (!ok) {
        qDebug() << "命令错误!!" << endl;
        return;
    }
    switch (num) {

    /* 登录 */
    case LOGIN:
    {
        qDebug() << "登录..." << endl;
        QStringList s = data.split(" ");
        QString PhoneNumStr = s.at(0);
        QString PasswordStr = s.at(1);
        bool ok;
        Usr usr = this->parent->getMan()->QueryUsrFromDatabase(PhoneNumStr, &ok);
        if (!ok) {

            /* 未找到用户 */
            this->sendInfo(NOT_FOUNT_USR);
            qDebug() << "未找到用户!" << endl;
            break;

        }
        if (usr.getPassword() == PasswordStr) {

            /* 登录成功 */
            this->sendInfo(LOGIN_OK);
            qDebug() << "登录成功!" << endl;
            break;
        }

        /* 密码错误 */
        this->sendInfo(PASSWORD_ERR);
        qDebug() << "密码错误!" << endl;
    }break;

    /* 注册 */
    case JOIN:
    {
        qDebug() << "注册..." << endl;
        QStringList s = data.split(" ");
        QString UsrNameStr  = s.at(0);
        QString PhoneNumStr = s.at(1);
        QString PasswordStr = s.at(2);

        Usr usr(UsrNameStr);
        usr.setPhoneNum(PhoneNumStr);
        usr.setPassword(PasswordStr);
        this->man->AddUsrToDatabase(usr);

        this->sendInfo(JOIN_OK);
        qDebug() << "注册成功" << endl;

    }break;

    /* 预约 */
    case RESERVATION:
    {
        /* 测试时是停车功能 */
        qDebug() << "预约..." << endl;
        /* 设置预约车辆 */
        Car newCar(data);
        newCar.setReservation(true);
#if 1
        //临时测试功能
        newCar.setEntryTime(QDateTime::currentDateTime());
        this->man->setCar(newCar);
#endif

        /* 获取空车位 */
        u_long num = this->man->getEmptySpace();
        if (0 == num) {
            qDebug() << "没有空车位!" << endl;
            this->sendInfo(RESERVATION_ERR, "No empty parking space!");
            break;
        }

        /* 设置车位号 */
        newCar.setParkingNum(num);

        /* 将预约车辆添加到数据库 */
        this->man->AddCarToDatabase(newCar);

        /* 更新ui */
        this->updateUI();

        /* 发送数据到客户端,发送车位号 */
        this->sendInfo(RESERVATION_OK, "Pleace park in parking space number: " + QString::number(num));
        qDebug() << "预约成功!" << endl;
    }break;

    /* 支付 */
    case PAY:
    {
        qDebug() << "支付" << endl;
        Car newCar = this->man->getCar();
        newCar.setPaymentStatus(true);
        this->man->UpdateCarFromDatabase(newCar);
        this->sendInfo(PAY_OK);
    }break;
    /* 注销 */
    case DELETE_USR:
    {
        qDebug() << "注销" << endl;
        this->man->DeleteUsrFromDatabase(data);
        this->sendInfo(DELETE_USR_OK);
    }break;
    /* 取车 */
    case PICK_CAR:
    {
        qDebug() << "取车..." << endl;
        Car delCar = this->man->QueryCarFromDatabase(data);
        delCar.setLeaveTime(QDateTime::currentDateTime());
        delCar.setCost(PRICE);
        delCar.setParkingTime();
        this->man->setCar(delCar);
        qDebug() << "ParkingNum:" << delCar.getParkingNum() << endl;
        this->man->FreeParkingSpace(delCar.getParkingNum());
        this->updateUI();
        this->man->DeleteCarFromDatabase(delCar.getCarNum());
        double cost = this->man->getCar().getCost();
        this->sendInfo(PICK_CAR_OK,"Please pay :" + (cost ? QString::number(cost) : "0") + "yuan.");
        this->sendInfo(SHOW,"Number of remainning parking space: " + QString::number(this->man->getEmptySpaceNum()));
        qDebug() << "取车成功!" << endl;
    }break;

    default:
        qDebug() << "未知命令!" << endl;
        break;
    }
}

void myThread::run()
{
    this->Socket = this->parent->getServer()->nextPendingConnection();
    //this->connect();
}

void myThread::sendInfo(int cmd, const QString &data)
{
    QString str = QString::number(cmd);
    str += ":";
    str += data;
    this->Socket->write(str.toUtf8());
}
